package com.status;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/4/14
 */

@SpringBootApplication
public class Application implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    // 状态机对象
    @Autowired
    private StateMachine<States, Events> stateMachine;

    @Override
    public void run(String... args){
        stateMachine.start();
        stateMachine.sendEvent(Events.PAY);
        stateMachine.sendEvent(Events.RECEIVE);
    }



    @Autowired
    private StateMachinePersister<States, Events, String> stateMachineMemPersister;

    //在run函数中，我们定义了整个流程的处理过程
//    @Override
//    public void run(String... args) throws Exception {
//        // 创建第1个订单对象
//        OrderPo order1 = new OrderPo();
//        order1.setStates(States.UNPAID);
//        order1.setId(1);
//
//        System.out.println("--- 发送第1个订单支付事件 ---");
//        boolean result = this.sendEvent(Events.PAY, order1);
//        System.out.println("> 事件是否发送成功：" + result
//                + "，订单编号：" + order1.getId()
//                +"，当前状态：" + stateMachine.getState().getId());
//
//        // 在子线程中等待6秒后再发送收货事件
//        new Thread(() -> {
//            try {
//                Thread.sleep(6000);
//                System.out.println("--- 发送第1个订单收货事件 ---");
//                boolean result2 = this.sendEvent(Events.RECEIVE, order1);
//                System.out.println("> 事件是否发送成功：" + result2
//                        + "，订单编号：" + order1.getId()
//                        +"，当前状态：" + stateMachine.getState().getId());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }).start();
//
//        // 创建第2个订单对象
//        OrderPo order2 = new OrderPo();
//        order2.setStates(States.UNPAID);
//        order2.setId(2);
//
//        System.out.println("--- 发送第2个订单支付事件 ---");
//        result = this.sendEvent(Events.PAY, order2);
//        System.out.println("> 事件是否发送成功：" + result
//                + "，订单编号：" + order2.getId()
//                +"，当前状态：" + stateMachine.getState().getId());
//
//        System.out.println("--- 发送第2个订单收货事件 ---");
//        result = this.sendEvent(Events.RECEIVE, order2);
//        System.out.println("> 事件是否发送成功：" + result
//                + "，订单编号：" + order2.getId()
//                +"，当前状态：" + stateMachine.getState().getId());
//
//    }

    /**
     * 发送订单状态转换事件
     * synchronized修饰保证这个方法是线程安全的
     */
    private synchronized boolean sendEvent(Events changeEvent, OrderPo order) {
        boolean result = false;
        try {
            //启动状态机
            stateMachine.start();
            //尝试恢复状态机状态
            stateMachineMemPersister.restore(stateMachine, String.valueOf(order.getId()));
            Message message = MessageBuilder.withPayload(changeEvent)
                    .setHeader("order", order).build();
            result = stateMachine.sendEvent(message);
            //持久化状态机状态
            stateMachineMemPersister.persist(stateMachine, String.valueOf(order.getId()));
        } catch (Exception e) {
            System.out.println("操作失败：" + e.getMessage());
        } finally {
            stateMachine.stop();
        }
        return result;
    }
}
